﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using log4net;
using ScpControl.Driver;
using ScpControl.Driver.PNPUtilLib;

namespace ScpCleanWipe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static readonly IDriverStore DrvStore = new PnpUtil();
        private static readonly X509Store CertStore = new X509Store("root", StoreLocation.CurrentUser);

        public MainWindow()
        {
            InitializeComponent();

            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                MessageBox.Show(((Exception) args.ExceptionObject).Message, "Exception",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            };
        }

        #region Windows Service Helpers

        private static bool StopService(string service)
        {
            try
            {
                var sc = new ServiceController(service);

                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    Thread.Sleep(1000);
                    return true;
                }
            }
            catch (InvalidOperationException iopex)
            {
                if (!(iopex.InnerException is Win32Exception))
                {
                    Log.ErrorFormat("Win32-Exception occured: {0}", iopex);
                    return false;
                }

                switch (((Win32Exception)iopex.InnerException).NativeErrorCode)
                {
                    case 1060: // ERROR_SERVICE_DOES_NOT_EXIST
                        Log.Warn("Service doesn't exist, maybe it was uninstalled before");
                        break;
                    default:
                        Log.ErrorFormat("Win32-Error: {0}", (Win32Exception)iopex.InnerException);
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Couldn't stop service: {0}", ex);
            }

            return false;
        }

        #endregion

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            MainButton.IsEnabled = !MainButton.IsEnabled;

            await Task.Run(() =>
            {
                #region Service tasks

                Log.InfoFormat("Stopping \"SCP DS3 Service\"...");
                StopService("SCP DS3 Service");

                Log.InfoFormat("Stopping \"SCP DSx Service\"...");
                StopService("SCP DSx Service");

                Log.InfoFormat("Searching for running processes...");
                foreach (var proc in Process.GetProcessesByName("Ds3Service"))
                {
                    Log.InfoFormat("Killing process: {0}", proc.ProcessName);
                    proc.Kill();
                }

                foreach (var proc in Process.GetProcessesByName("DsxService"))
                {
                    Log.InfoFormat("Killing process: {0}", proc.ProcessName);
                    proc.Kill();
                }

                Log.InfoFormat("Removing service...");
                Process.Start("sc", "delete Ds3Service").WaitForExit();

                Process.Start("sc", "delete DsxService").WaitForExit();

                #endregion

                #region Driver store clean-up

                Log.InfoFormat("Searching the driver store...");
                var storeEntries = DrvStore.EnumeratePackages();

                foreach (var entry in storeEntries.Where(dse => dse.DriverPkgProvider.Equals("Scarlet.Crush Productions")))
                {
                    Log.InfoFormat("Removing package from driver store: {0} by {1}", entry.DriverPublishedName, entry.DriverPkgProvider);
                    DrvStore.DeletePackage(entry, true);
                }

                foreach (var entry in storeEntries.Where(dse => dse.DriverSignerName.Contains("libwdi autogenerated")
                                                           && dse.DriverPkgProvider.Equals("libusbK")))
                {
                    Log.InfoFormat("Removing package from driver store: {0} by {1}", entry.DriverPublishedName, entry.DriverPkgProvider);
                    DrvStore.DeletePackage(entry, true);
                }

                foreach (var entry in storeEntries.Where(dse => dse.DriverPkgProvider.Contains("MotioninJoy")))
                {
                    Log.InfoFormat("Removing package from driver store: {0} by {1}", entry.DriverPublishedName, entry.DriverPkgProvider);
                    DrvStore.DeletePackage(entry, true);
                }

                #endregion

                #region Driver uninstallation

                string devPath = string.Empty;
                string instanceId = string.Empty;
                bool rebootRequired = false;

                DriverInstaller.UninstallBluetoothDongles(ref rebootRequired);

                DriverInstaller.UninstallDualShock3Controllers(ref rebootRequired);

                DriverInstaller.UninstallDualShock4Controllers(ref rebootRequired);

                if (Devcon.Find(Guid.Parse("f679f562-3164-42ce-a4db-e7ddbe723909"), ref devPath, ref instanceId))
                {
                    if (Devcon.Remove(Guid.Parse("f679f562-3164-42ce-a4db-e7ddbe723909"), devPath, instanceId))
                    {
                        Difx.Instance.Uninstall(Path.Combine(@".\System\", @"ScpVBus.inf"),
                            DifxFlags.DRIVER_PACKAGE_DELETE_FILES,
                            out rebootRequired);
                    }
                }

                while (Devcon.Find(Guid.Parse("2F87C733-60E0-4355-8515-95D6978418B2"), ref devPath, ref instanceId))
                {
                    Devcon.Remove(Guid.Parse("2F87C733-60E0-4355-8515-95D6978418B2"), devPath, instanceId);
                }

                while (Devcon.Find(Guid.Parse("E2824A09-DBAA-4407-85CA-C8E8FF5F6FFA"), ref devPath, ref instanceId))
                {
                    Devcon.Remove(Guid.Parse("E2824A09-DBAA-4407-85CA-C8E8FF5F6FFA"), devPath, instanceId);
                }

                while (Devcon.Find(Guid.Parse("2ED90CE1-376F-4982-8F7F-E056CBC3CA71"), ref devPath, ref instanceId))
                {
                    Devcon.Remove(Guid.Parse("2ED90CE1-376F-4982-8F7F-E056CBC3CA71"), devPath, instanceId);
                }

                Devcon.Refresh();

                #endregion

                #region Cert store clean-up

                CertStore.Open(OpenFlags.MaxAllowed);

                foreach (var cert in CertStore.Certificates.Cast<X509Certificate2>().Where(c => c.FriendlyName.Contains("libwdi")))
                {
                    Log.InfoFormat("Removing certificate from root certificate store: {0}", cert.SubjectName.Name);
                    CertStore.Remove(cert);
                }

                //Close the store.
                CertStore.Close();

                #endregion
            });

            MainButton.IsEnabled = !MainButton.IsEnabled;

            MessageBox.Show("All steps finished, now try ScpDriverInstaller again! Good luck :)", "Finished",
                MessageBoxButton.OK, MessageBoxImage.Information);

            Close();
        }
    }
}
